<template>
    <modal ref="modal_1"
           :title="title"
           :isCloseConfirm="false"
           top="5%"
           width="50%"
           height="50%"
           :close-on-click-modal="false"
           @modalOpened="opened"
           @modalClose="close">
        <avue-crud :option="option"
                   :table-loading="loading"
                   :data="data"
                   v-model:page="page"
                   :permission="permissionList"
                   v-model="form"
                   ref="crud"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @sort-change="sortChange"
                   @row-dblclick="dblclick"
                   @selection-change="selectionChange"
                   @size-change="sizeChange"
                   @refresh-change="refreshChange"
                   @on-load="onLoad">
        </avue-crud>
        <template #footerButton>
            <el-button type="primary" icon="el-icon-plus"  @click="handleSubmit">确定</el-button>
            <el-button icon="el-icon-circle-close" @click="$refs.modal_1.handleClose()">关闭</el-button>
        </template>
    </modal>
</template>

<script>
    import {getList, getDetail, add, update, remove} from "@/api/anwise/prod/prod_summary";
    import {mapGetters} from "vuex";
    import 'nprogress/nprogress.css';

    export default {
        data() {
            return {
                form: {},
                query: {},
                loading: true,
                page: {
                    pageSize: 20,
                    currentPage: 1,
                    total: 0
                },
                selectionList: [],
                option: {
                    height:'auto',
                    calcHeight: 30,
                    tip: false,
                    searchShow: true,
                    searchMenuSpan: 6,
                    border: true,
                    index: true,
                    rowKey: 'id',
                    selection: true,
                    reserveSelection:true,
                    searchLabelWidth:120,
                    searchSpan:8,
                    labelWidth: 150,
                    menuWidth: 120,
                    indexWidth:100,
                    menu:false,
                    addBtn: false,
                    editBtn: false,
                    viewBtn: false,
                    delBtn: false,
                    stripe: true,
                    searchShowBtn:true,
                    searchIndex: 3,
                    searchIcon: false,
                    searchEnter:true,
                    selectable:(row,index)=>{
                        return !this.arrayList.some((val) =>(val.aboutNo === row.tabNo && val.materialNo=== row.materialNo))
                    },
                    column: [
                        {
                            label: "加工商编号",
                            prop: "supplierNo",
                            type: "input",
                            search:true,
                            overHidden: true,
                            sortable: true,
                            width:140,
                        },
                        {
                            label: "加工商名称",
                            prop: "supplierName",
                            type: "input",
                            search:true,
                            overHidden: true,
                            sortable: true,
                            width:200,
                        },
                        {
                            label: "生产单号",
                            prop: "tabNo",
                            type: "input",
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:150,
                        },
                        {
                            label: "所需物料编号",
                            prop: "materialNo",
                            type: "input",
                            search:true,
                            overHidden: true,
                            sortable: true,
                            width:150,
                        },
                        {
                            label: "所需物料名称",
                            prop: "materialName",
                            type: "input",
                            search:true,
                            overHidden: true,
                            sortable: true,
                            width:200,
                        },
                        {
                            label: "物料规格",
                            prop: "specification",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:120,
                        },
                        {
                            label: "排单日期",
                            prop: "tabDate",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:120,
                            // search: true,
                        },
                        {
                            label: "物料类别",
                            prop: "category",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:120,
                        },
                        {
                            label: "单位",
                            prop: "unit",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:120,
                        },
                        {
                            label: "汇总数量",
                            prop: "num",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:120,
                        },
                        {
                            label: "可领数量",
                            prop: "needNum",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:120,
                        },
                        {
                            label: "仓存数量",
                            prop: "totalStockNum",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:120,
                        },
                        {
                            label: "箱唛",
                            prop: "packLabel",
                            type: "textarea",
                            span: 24,
                            minRows:2,
                            width:120,
                            overHidden: true,
                            sortable: true,
                        },
                        {
                            label: "箱唛颜色",
                            prop: "labelColor",
                            type: "textarea",
                            span: 24,
                            minRows:2,
                            width:120,
                            overHidden: true,
                            sortable: true,
                        },
                    ]
                },
                data: []
            };
        },
        computed: {
            ...mapGetters(["permission"]),
            permissionList() {
                return {
                    viewBtn: this.validData(this.permission.prod_summary_view, false),
                };
            },
            ids() {
                let ids = [];
                this.selectionList.forEach(ele => {
                    ids.push(ele.id);
                });
                return ids.join(",");
            }
        },
        methods: {
            selectionLoad() {
                let arr = [];
                // console.log(this.selectionList);
                // if (this.selectionList && this.selectionList != null
                //     && this.selectionList.length > 0) {
                //     this.data.forEach(row => {
                //         if(this.selectionList.some((val) => val.tabNo === row.tabNo && val.materialNo=== row.materialNo)) {
                //             console.log("row.id1+"+row.id)
                //             arr.push(row);
                //         }
                //     })
                // } else {
                    console.log("arrayList",this.arrayList);
                    if (this.arrayList) {
                        this.data.forEach(row => {
                            if(this.arrayList.some((val) => val.aboutNo === row.tabNo && val.materialNo=== row.materialNo)) {
                                console.log("row.id2+"+row.id)
                                arr.push(row);
                            }
                        })

                    }
                // }
                this.$nextTick(() => {
                    arr.forEach(item => {
                        // this.$refs.crud.clearSelection();
                        console.log("check"+item.id)
                        this.$refs.crud.toggleRowSelection(item, true);
                    })
                })
            },
            handleSubmit() {
                this.$refs.modal_1.hide();
                this.$emit("setProdSummary",this.selectionList);

                // if (this.isAdd) {
                //   this.$refs.modal_1.handleClose()
                //   // this.$emit("success", this.selectionList);
                // } else if (this.isListAdd) {
                //
                //   // this.$emit("success", this.versionId, this.selectionList);
                // }
            },
            showSelect(list,deptId) {
                this.deptId = deptId;
                this.arrayList = list;
                this.selectionList = [];
                this.$refs.modal_1.show();
                // this.onLoad(this.page);
            },
            clearData() {
                this.data = [];
                this.page.currentPage = 1;
            },
            clearSelection() {
                this.selectionList = [];
                this.arrayList = [];
                this.$refs.crud.toggleSelection();
            },
            close() {
                this.clearData();
                this.clearSelection();
            },
            rowSave(row, done, loading) {
                add(row).then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                    done();
                }, error => {
                    loading();
                    window.console.log(error);
                });
            },
            rowUpdate(row, index, done, loading) {
                update(row).then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                    done();
                }, error => {
                    loading();
                    window.console.log(error);
                });
            },
            rowDel(row) {
                this.$confirm("确定将选择数据删除?", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        return remove(row.id);
                    })
                    .then(() => {
                        this.onLoad(this.page);
                        this.$message({
                            type: "success",
                            message: "操作成功!"
                        });
                    });
            },
            handleDelete() {
                if (this.selectionList.length === 0) {
                    this.$message.warning("请选择至少一条数据");
                    return;
                }
                this.$confirm("确定将选择数据删除?", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        return remove(this.ids);
                    })
                    .then(() => {
                        this.onLoad(this.page);
                        this.$message({
                            type: "success",
                            message: "操作成功!"
                        });
                        this.$refs.crud.toggleSelection();
                    });
            },
            beforeOpen(done, type) {
                if (["edit", "view"].includes(type)) {
                    getDetail(this.form.id).then(res => {
                        this.form = res.data.data;
                    });
                }
                done();
            },
            sortChange(val) {
                console.log(val)
                this.sort = {
                    prop: val.prop,
                    order: val.order
                };
                let order = "";
                if (this.sort.prop && this.sort.order) {
                    if (this.sort.order === "descending") {
                        order = `${this.sort.prop} desc`
                    } else {
                        order = `${this.sort.prop} asc`
                    }
                }
                this.query.orderBy = order;
                this.onLoad(this.page);
            },
            searchReset() {
                this.query = {};
                this.onLoad(this.page);
            },
            searchChange(params, done) {
                this.query = params;
                this.page.currentPage = 1
                this.onLoad(this.page, params);
                done();
            },
            selectionChange(list) {
                this.selectionList = list;
            },
            selectionClear() {
                this.selectionList = [];
                this.$refs.crud.toggleSelection();
            },
            currentChange(currentPage){
                this.page.currentPage = currentPage;
            },
            sizeChange(pageSize){
                this.page.pageSize = pageSize;
            },
            refreshChange() {
                this.onLoad(this.page, this.query);
            },
            onLoad(page, params = {}) {
                this.loading = true;
                params.deptId = this.deptId;
                params.searchType = 'select';
                getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
                    const data = res.data.data;
                    this.page.total = data.total;
                    this.data = data.records;
                    this.loading = false;
                    this.selectionLoad();
                });
            }
        }
    };
</script>

<style>
</style>
